package com.example.demo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.servicedemo.IMathService;


public class MainActivity extends AppCompatActivity {
    private IMathService mathService;
    private final ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            mathService = IMathService.Stub.asInterface(iBinder);

        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            mathService = null;
        }
    };
    private boolean isBound = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textView = findViewById(R.id.result_textView);
        Button bindButton = findViewById(R.id.button_bind);
        Button unbindButton = findViewById(R.id.button_unbind);
        Button computeButton = findViewById(R.id.button_compute);

        bindButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isBound){
                    Toast.makeText(MainActivity.this, "已经绑定", Toast.LENGTH_SHORT).show();
                }else{
                    //action
                    Intent serviceIntent = new Intent("com.example.servicedemo.math");
                    //服务端包名
                    serviceIntent.setPackage("com.example.servicedemo");
//                final Intent serviceIntent = new Intent(MainActivity.this,
//                                com.example.serviceDemo.MathService.class);
                    bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE);
                    isBound = true;
                }
            }
        });

        unbindButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isBound){
                    unbindService(connection);
                    isBound = false;
                }else{
                    Toast.makeText(MainActivity.this, "未绑定", Toast.LENGTH_SHORT).show();
                }
            }
        });

        computeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!isBound){
                    Toast.makeText(MainActivity.this, "未绑定", Toast.LENGTH_SHORT).show();
                    return;
                }
                long a = Math.round(Math.random() * 100);
                long b = Math.round(Math.random() * 100);
                try {
                    long res = mathService.add(a, b);
                    textView.setText(a + " + " + b + " = " + res);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }

            }
        });
    }
}